{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:channel type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <input type="text" class="layui-input" id="date" placeholder="回款日期">
        <input type="hidden" id="receipt_time" value="">
    </div>
    <div class="layui-inline">
        <select name="" id="balance_type" lay-filter="balance_type">
            <option value="">结账类型</option>
            <option value="1">公账</option>
            <option value="2">私账</option>
        </select>
    </div>
    <div class="layui-inline" style="width: 800px;">
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="add">生成结算单</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="receive">已核对</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="urge">催款</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="finish">已回款</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="export">导出</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="statusTpl">
    {{#  if(d.status === '生成中'){ }}
    <span class="layui-bg-cyan status1" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已提交') { }}
    <span class="layui-bg-orange status2" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已核对') { }}
    <span class="layui-bg-blue status3" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已回款') { }}
    <span class="layui-bg-green status4" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } }}

    {{#  if(d.urge===1 && d.status==='已核对'){ }}
    <span class="layui-bg-red status5" style="padding: 4px 6px;">催款</span>
    {{#  } }}

    {{#  if(d.send_time!=='' && d.send_info!==''){ }}
    <span class="layui-bg-cyan" style="padding: 4px 6px;">已寄件</span>
    {{#  } }}
</script>

<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="info">填写</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="view">查看</a>
    {{# if(d.status == '已转账'){ }}
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="del">删除</button>
    {{#  }else{ }}
    <button class="layui-btn layui-btn-green layui-btn-sm status6" val="{{ d.id }}" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-danger layui-btn-sm status7" val="{{ d.id }}" lay-event="del">删除</button>
    {{#  } }}
</script>
{/block}

{block name="css"}
<style>
    table {border-collapse: separate;}
    .layui-table-cell {height: 62px; line-height:62px;}
    thead .layui-table-cell {line-height: 28px; height: 28px;}
</style>
{/block}

{block name="js"}
<script>
    layui.use(['table', 'laydate'], function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,laydate = layui.laydate
            ,active = {
                reload: function () {
                    var channel_id = $('#channel_id').val()
                        ,receipt_time= $('#receipt_time').val()
                        ,balance_type = $('#balance_type').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        , where: {channel_id:channel_id,receipt_time:receipt_time,balance_type:balance_type}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("AuditSdkMng/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {type:'checkbox', fixed: 'left'}
                ,{field:'id', title: 'ID', width: 60, sort:true, fixed: 'left'}
                ,{field:'balance_type', title: '类型', width: 60, fixed: 'left'}
                ,{field:'daytime', title: '对账日期', width: 170, sort:true, fixed: 'left'}
                ,{field:'channel_id', title: '渠道名称', width: 140, sort:true, fixed: 'left'}
                ,{field:'game_name', title: '游戏', width: 80}
                ,{field:'other_fixed_price', title: '充值金额', width: 100, sort:true, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'sdk_fixed_price', title: '后台金额', width: 100, sort:true, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'diff_price', title: '差额', width: 80, sort:true}
                ,{field:'pay_rate', title: '分成', width: 60}
                ,{field:'channel_rate', title: '费率', width: 60}
                ,{field:'tax_rate', title: '税率', width: 65}
                ,{field:'pay_price', title: '需实结金额', width: 110, sort:true, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'balance_time', title: '对账日期', width: 100, sort:true}
                ,{field:'receipt_time', title: '回款日期', width: 100, sort:true}
                ,{field:'receipt_tag', title: '标签', width: 60}
                ,{field:'receipt_price', title: '未回款金额', width: 120, sort:true}
                ,{field:'open_invoice', title: '是否开票', width: 90}
                ,{field:'send_time', title: '寄件日期', width: 100, sort:true}
                ,{field:'send_info', title: '快递信息', width: 200, sort:true}
                ,{field:'company', title: '公司名称', width: 200, sort:true}
                ,{field:'status', title: '状态', width: 180, sort:true, templet: '#statusTpl', fixed: 'right'}
                ,{title: '操作', align:'center', width: 250, toolbar:'#tableBar', fixed: 'right'}
            ]]
        });

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("AuditSdkMng/edit")}',{id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });

        //监听搜索栏
        $('#searchBox .toolBtn').on('click', function(){
            if($(this).attr('id')=='add'){
                layer.open({
                    type: 2
                    ,title: '添加'
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditSdkMng/add")}'
                });
            }else if($(this).attr('id')=='receive'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditSdkMng/receive")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status2[val="'+data[key].id+'"]').text('已核对').addClass('layui-bg-blue status3').removeClass('layui-bg-orange status2');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='finish'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditSdkMng/finish")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status3[val="'+data[key].id+'"]').siblings().remove();
                            $('.status3[val="'+data[key].id+'"]').text('已回款').addClass('layui-bg-green status4').removeClass('layui-bg-blue status3');
                            $('.status6[val="'+data[key].id+'"]').addClass('layui-btn-disabled').removeClass('layui-btn-green').attr('disabled', true);
                            $('.status7[val="'+data[key].id+'"]').addClass('layui-btn-disabled').removeClass('layui-btn-danger').attr('disabled', true);

                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='urge'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditSdkMng/urge")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status3[val="'+data[key].id+'"]').next('.status5').remove();
                            $('.status3[val="'+data[key].id+'"]').after(' <span class="layui-bg-red status5" style="padding: 4px 6px;">催款</span>');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='export'){
                var jsonStr = ''
                    ,jsonObj = {
                        "channel_id": $('#channel_id').val()
                        ,"receipt_time": $('#receipt_time').val()
                        ,"balance_type": $('#balance_type').val()
                    };
                layer.msg('导出中....', {icon: 1, time: 2000});
                for(k in jsonObj){
                    jsonStr += k + '=' + jsonObj[k] + '&';
                }
                window.location.href = '{:url("AuditSdkMng/export")}?'+jsonStr.substring(0, jsonStr.length-1);
            }
        })

        //监听搜索下拉菜单
        form.on('select(channel_id)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听搜索下拉菜单
        form.on('select(balance_type)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听时间搜索
        laydate.render({
            elem: '#date'
            ,type: 'date'
            ,range: true
            ,done: function(value, date, endDate){
                $('#receipt_time').val(value);
                load = layer.load();
                active['reload'].call(this);
            }
        });

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditSdkMng/edit")}?id='+data.id
                    ,cancel: function(){
                        $.post('{:url("AuditSdkMng/index")}',{id: data.id}, function(json){
                            obj.update({
                                cps_pay_price: json.data[0].cps_pay_price
                                ,sdk_pay_price: json.data[0].sdk_pay_price
                                ,cps_fixed_price: json.data[0].cps_fixed_price
                                ,sdk_fixed_price: json.data[0].sdk_fixed_price
                                ,channel_rate: json.data[0].channel_rate
                                ,tax_rate: json.data[0].tax_rate
                                ,pay_rate: json.data[0].pay_rate
                                ,cps_total_price: json.data[0].cps_total_price
                                ,sdk_total_price: json.data[0].sdk_total_price
                                ,all_total_price: json.data[0].all_total_price
                                ,invoice_type: json.data[0].invoice_type
                                ,balance_time: json.data[0].balance_time
                                ,receipt_time: json.data[0].receipt_time
                                ,status: json.data[0].status
                                ,cps_diff_price: json.data[0].cps_diff_price
                                ,sdk_diff_price: json.data[0].sdk_diff_price
                                ,remark: json.data[0].remark
                                ,urge: json.data[0].urge
                                ,balance_type: json.data[0].balance_type
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("AuditSdkMng/del")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if(obj.event === 'view'){
                layer.open({
                    type: 2
                    ,title: '查看'
                    ,offset: '100px'
                    ,resize: true
                    ,area: ['1200px', '700px']
                    ,content: '{:url("AuditSdkMng/view")}?id='+data.id
                });
            }else if(obj.event === 'info'){
                layer.open({
                    type: 2
                    ,title: '填写信息'
                    ,area: ['1000px', '650px']
                    ,offset: '100px'
                    ,resize: true
                    ,content: '{:url("AuditSdkMng/info")}?id='+data.id
                    ,cancel: function(){
                        $.post('{:url("AuditSdkMng/index")}',{id: data.id}, function(json){
                            obj.update({
                                other_fixed_price: json.data[0].other_fixed_price
                                ,diff_price: json.data[0].diff_price
                                ,open_invoice: json.data[0].open_invoice
                                ,send_time: json.data[0].send_time
                                ,send_info: json.data[0].send_info
                                ,pay_price: json.data[0].pay_price
                                ,all_total_price: json.data[0].all_total_price
                                ,status: json.data[0].status
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }
        });
    });
</script>
{/block}